package com.flyemu.share.controller;

import cn.hutool.core.lang.Assert;
import com.flyemu.share.entity.Organization;
import com.flyemu.share.service.OrganizationService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * @功能描述: 组织机构管理
 * @创建时间: 2023年08月08日
 * @公司官网: www.fenxi365.com
 * @公司信息: 纷析云（杭州）科技有限公司
 * @公司介绍: 专注于财务相关软件开发, 企业会计自动化解决方案
 */
@RestController
@RequestMapping("/organization")
@RequiredArgsConstructor
public class OrganizationController {

    private final OrganizationService organizationService;


    @GetMapping
    public JsonResult list(OrganizationService.Query query) {
        return JsonResult.successful(organizationService.query(query));
    }

    @GetMapping("all")
    public JsonResult listAll(Integer merchantId) {
        return JsonResult.successful(organizationService.listAll(merchantId));
    }


    @PostMapping
    public JsonResult save(@RequestBody @Valid Organization organization) {
        Assert.isNull(organization.getId(), "新增组织Id必须为空~");
        organizationService.save(organization);
        return JsonResult.successful();
    }


    @PutMapping
    public JsonResult update(@RequestBody @Valid Organization organization) {
        Assert.notNull(organization.getId(), "更新组织Id不允许为空~");
        organizationService.save(organization);
        return JsonResult.successful();
    }


    @DeleteMapping("/{organizationId}")
    public JsonResult delete(@PathVariable Integer organizationId) {
        organizationService.delete(organizationId);
        return JsonResult.successful();
    }

}
